package com.hcbd.tdwt.ui.haige.common.fragment;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.core.content.ContextCompat;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.bd.BDNotificationManager;
import com.hcbd.tdwt.ui.fuda.common.activity.SendMsgActivity_;

import com.hcbd.tdwt.ui.fuda.setting.activity.AuthInfoActivity_;
import com.hcbd.tdwt.ui.fuda.setting.activity.ChannelSetActivity_;
import com.hcbd.tdwt.ui.fuda.setting.activity.HelpCenterActivity_;
import com.hcbd.tdwt.ui.fuda.setting.activity.PositionReportingActivity_;

import com.hcbd.tdwt.ui.haige.common.activity.BdStatusActivity_;
import com.hcbd.tdwt.ui.haige.setting.activity.AboutUsActivity_;
import com.hcbd.tdwt.ui.haige.setting.activity.PresetContentActivity_;
import com.hcbd.tdwt.ui.haige.setting.activity.SendSetActivity_;
import com.hcbd.tdwt.ui.haige.setting.activity.SunriseAndSunsetActivity_;

import com.hcbd.tdwt.base.AuthManger;
import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.pangu.bdsdk2021.entity.terminalthree.BDSXXInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDSnrInfo;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.text.MessageFormat;
import java.util.Objects;
@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.fragment_my_haige)
public class MyFragment extends BaseFragment {
    @ViewById
    TextView title;
    @ViewById
    ImageView headLeft;
    @ViewById
    TextView contentTv, cardNumTv, frequencyTV, signalLockTv, signalTv, activationTV, account, contentInfo;
    @ViewById
    LinearLayout bdInfoLL, deviceLL;
    @ViewById
    RelativeLayout topRL;
    final private int refresh_code = 5;
    private Drawable brownGreenBg;
    private Drawable scarletBg;
    private int red_400_color;
    private int while_color;
    @AfterViews
    protected void init() {
        title.setText("我的");
        topRL.setBackground(getContext().getDrawable(R.drawable.top_bg_haige_flag));
        brownGreenBg = getContext().getDrawable(R.drawable.ova_brown_green_bg);
        scarletBg = getContext().getDrawable(R.drawable.ova_red_300_bg);
        red_400_color=getContext().getColor(R.color.red_400);
        while_color=getContext().getColor(R.color.white);
        headLeft.setVisibility(View.GONE);
        if (AuthManger.getAuthManger().isPicOnline() && AuthManger.getAuthManger().isVoiceOnline()) {
            activationTV.setText("已激活");
            activationTV.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
        } else {
            activationTV.setText("未激活");
            activationTV.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
        }
        initMsgRegister();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDSnrInfo bdSnrInfo) {
        updateBSIData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDSXXInfo bdsxxInfo) {
        refreshStatus();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    public void refreshStatus() {
        if (BeidouBoxParams.isBoxConnectNormal) {
            contentTv.setText("已连接");
            contentTv.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.layout_frame_brown_green));
            contentTv.setTextColor(while_color);
            cardNumTv.setText(MessageFormat.format("北斗卡号：{0}", BeidouBoxParams.userCardNumber));
            frequencyTV.setText(MessageFormat.format("通信频度：{0} 秒", BeidouBoxParams.sendFreq));
            account.setText(String.format("%s", BeidouBoxParams.userCardNumber));
            contentInfo.setText("关闭北斗服务");
            contentInfo.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.open_bg_haige));
        } else {
            contentTv.setText("未连接");
            contentTv.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.layout_frame_red_300));
            contentTv.setTextColor(red_400_color);
            cardNumTv.setText(MessageFormat.format("北斗卡号：{0}", "-"));
            frequencyTV.setText(MessageFormat.format("通信频度：{0}", "-"));
            account.setText("--");
            contentInfo.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.not_open_bg_haige));
            contentInfo.setText("打开北斗服务");
        }


    }

    public void updateBSIData() {
        if (BeidouBoxParams.isBoxConnectNormal) {
            signalLockTv.setText("锁定");
            signalLockTv.setBackground(brownGreenBg);
            signalLockTv.setTextColor(while_color);
            if (BeidouBoxParams.getBdSignal() > 2) {
                signalTv.setBackground(brownGreenBg);
                signalTv.setText("强");
                signalTv.setTextColor(while_color);
            } else {
                signalTv.setBackground(scarletBg);
                signalTv.setText("弱");
                signalTv.setTextColor(red_400_color);
            }
        } else {
            signalLockTv.setText("无");
            signalLockTv.setTextColor(red_400_color);
            signalTv.setText("无");
            signalTv.setTextColor(red_400_color);
            signalLockTv.setBackground(scarletBg);
            signalTv.setBackground(scarletBg);
        }

    }



    @Click
    void sendMsgLL() {
        Intent intent = new Intent();
        intent.setClass(Objects.requireNonNull(getContext()), SendMsgActivity_.class);
        startActivityForResult(intent, refresh_code);
    }

    @Click
    void positionReportLL() {
        Intent intent = new Intent();
        intent.setClass(Objects.requireNonNull(getContext()), PositionReportingActivity_.class);
        startActivity(intent);
    }
    @Click
    void authInfoRL() {
        Intent intent = new Intent(getContext(), AuthInfoActivity_.class);
        startActivity(intent);
    }
    @Click
    void offMapLL() {
        startActivity(new Intent(this.getContext(), com.amap.api.maps.offlinemap.OfflineMapActivity.class));
    }

    @Click
    void sunriseLL() {
        Intent intent = new Intent(getContext(), SunriseAndSunsetActivity_.class);
        startActivity(intent);
    }

    @Click
    void channelSetLL() {
        Intent intent = new Intent(getContext(), ChannelSetActivity_.class);
        startActivity(intent);
    }



    @Click
    void helpCenterLL() {
        Intent intent = new Intent(getContext(), HelpCenterActivity_.class);
        startActivity(intent);
    }

    @Click
    void aboutLL() {
        Intent intent = new Intent(getContext(), AboutUsActivity_.class);
        startActivity(intent);
    }
    @Click
    void customLL(){
        Intent intent = new Intent(getContext(), PresetContentActivity_.class);
        startActivity(intent);
    }
   @Click
   void sendMsgSetRL(){
       Intent intent = new Intent(getContext(), SendSetActivity_.class);
       startActivity(intent);
   }

    @Click
    void accountLL() {
        if (BeidouBoxParams.isBoxConnectNormal) {
            BDNotificationManager.getInstance().clsoeBdcom();
            refreshStatus();
            updateBSIData();

        } else {
            BDNotificationManager.getInstance().openBdcom(getActivity());
        }
    }

    @Click
    void deviceLL() {
        Intent intent = new Intent(getContext(), BdStatusActivity_.class);
        startActivity(intent);
    }

    @Override
    public void  onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden){
            //不可见
            hideInputKeyBord();
            EventBus.getDefault().unregister(this);
        }else{
            EventBus.getDefault().register(this);
            updateBSIData();
        }
    }
}
